#pragma once
#include <windows.h>
#include <xnamath.h>
#include <cfloat>

struct BBox{
	BBox::BBox(){
		min = XMVectorSet(FLT_MAX, FLT_MAX, FLT_MAX, 1.f);
		max = XMVectorSet(-FLT_MAX, -FLT_MAX, -FLT_MAX, 1.f);
	}
	const BBox& operator &= (const BBox& box){
		min = XMVectorMin(min, box.min);
		max = XMVectorMax(max, box.max);
		return *this;
	}
	const BBox& operator &= (const XMVECTOR& v){
		min = XMVectorMin(min, v);
		max = XMVectorMax(max, v);
		return *this;
	}
	XMMATRIX GetMatrix(){
		auto mid = -(min + max)/2.f;
		auto trans = XMMatrixTranslation(mid.m128_f32[0], mid.m128_f32[1], mid.m128_f32[2]);

		XMVECTOR delta = XMVectorAbs(XMVectorSubtract(max, min));
		float longestLen = -FLT_MAX;
		for(auto f : delta.m128_f32) if(f > longestLen) longestLen = f;
		longestLen = 1.f/longestLen;
		auto scale = XMMatrixScaling(longestLen,longestLen,longestLen);
		return trans * scale;
	}
private:
	XMVECTOR min, max;
};




